package com.kyzh.core.pager.weal.address

import android.os.Bundle
import com.google.gson.Gson
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Address
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.requests.UserRequest
import com.gushenge.core.toast
import com.kyzh.core.R
import com.kyzh.core.utils.aesEcb
import kotlinx.android.synthetic.main.act_addressedit.*

class AddressEditActivity : BaseActivity(), ResultListener {
    var address:Address?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_addressedit)
        initUi()
    }

    private fun initUi() {
        titleView.setText(getString(R.string.modifyAddress))
        address = intent.getSerializableExtra(GlobalKeys.ADDRESS) as Address
        address?.let {
            name.setText(it.consignee)
            phone.setText(it.mobile)
            uaddress.setText(it.uaddress)
            aliAccount.setText(it.bank_id)
            aliName.setText(it.bank_user)
            switcher.isChecked = it.moren=="1"
        }
        commit.setOnClickListener {
            val name = name.text.toString()
            val phone = phone.text.toString()
            val uaddress = uaddress.text.toString()
            val aliAccount  = aliAccount.text.toString().trim()
            val aliName  = aliName.text.toString().trim()
            val moren = if (switcher.isChecked) "1" else "0"
            if (name.isNotEmpty() && phone.isNotEmpty()&& uaddress.isNotEmpty()){
                val map = HashMap<String,String>()
                map["consignee"] = name
                map["mobile"] = phone
                map["uaddress"] = uaddress
                map["moren"] = moren
                map["bank_id"] = aliAccount
                map["bank_user"] = aliName
                val value = aesEcb(Gson().toJson(map))
                UserRequest.changeAddress(value,address?.id?:""){
                    getString(R.string.modifySuccess).toast()
                    finish()
                }
            }else{
                getString(R.string.pleaseSelectHaveEmpty).toast()
            }
        }
    }

}
